/*
 * to use online script you can use jquery getScript plugin as follows:
 *
 * $.getScript('http://sisense.com/abc.js', function(data, textStatus)
 * {
 *		// your code which requires abc.js here
 * });
 *
 * // more info at: http://api.jquery.com/jQuery.getScript/
 *
 */


/*
 * set to true when replacing widget rendering engine.
 */
this.isCustom = true;

/*
 * exectuted after the widget was first initialized, useful when overriding entire widget rendering, i/e replacing the button with a whole different button ux
 * @param {args} wraps all function arguments, including widget, dashboard and widget controller instances.
 */
this.init = function (args) {


};

/*
 * executed before the widget query result is loaded to the DOM and allows canceling such beavior and replace it with custom widget implementation.
 * to cancel the default population just set 'args.cancel = true' and the widget default population will be ignored.
 * @param {args} wraps all function arguments, including query result, widget, dashboard and widget controller instances.
 */
this.beforeResultLoaded = function (args) {


};

/*
 * executed after the widget result were added to the DOM. useful to apply DOM tweaks after query result population.
 * @param {args} wraps all function arguments, including query result, widget, dashboard and widget controller instances.
 */
this.afterResultLoaded = function (args) {

    var colCountry = 0,
        colCity = 1,
        colAddress = 2,
        colSupplier = 3,
        colName = 4,
        targetWidget = args.widget.dashboard.getWidget('P5d7f3ea42412452a9227139c31379f');

    var rowscount = args.result.data.length;

    /* Define method to parse address from the pivot row */
    var getAddress = function (row) {

        return row[colAddress] + ', ' + row[colCity] + ', ' + row[colCountry];
    };

    /* Prepare Google Maps API script */
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "https://maps.googleapis.com/maps/api/js?key=AIzaSyATEZa2VBWm5kEj4Y97fFXT5g4Zf0pG--s&sensor=false&callback=runGoogleMap";
    document.body.appendChild(script);

    runGoogleMap = function () {

        $.getScript('/mapCoords.js', function() {

            var containerId = $(args.element).attr('id');

            var latlng = new google.maps.LatLng(0,0);
            var myOptions = {
              zoom: 1,
              center: latlng,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(args.element, myOptions);

            for (var i = 0; i < rowscount; i++) {

                var row = args.result.getRow(i);
                if (row) {

                    var address = getAddress(row),
                        id = row[colSupplier],
                        location = mapCoords[id],
                        position;

                    if (!location && failedCoords[id]) {

                        var tempCoords = failedCoords[id];
                        location = { Ua: tempCoords.location[0], Va: tempCoords.location[1]};
                    }

                    // If supplider coordinates aren't in our collection, let's ask
                    // the Google API for it.
                    if (!location) {

                        // Todo:
                    }
                    else {

                        position = new google.maps.LatLng(location.Ua, location.Va);
                        var marker = new google.maps.Marker({
                            map: map,
                            position: position,
                            title: id + ': ' + row[colName],
                            id: id,
                            address: address
                        });

                        /* Define marker selection callback: Change selection in the target widtet */

                        google.maps.event.addListener(marker, 'click', function() {

                            prism.WidgetClient.sendAction(targetWidget, {
                                action: DashboardActionHandler.WidgetSelectionHandler,
                                SelectedMembers: encodeURIComponent(JSON.stringify(this.id))
                            })
                        });
                    }
                }
            }
        });
    }

};

/*
 * exectuted after all dashboard widget models were initialized. useful for binding with other widget events.
 * @param {args} wraps all function arguments, including widget, dashboard and widget controller instances.
 */
this.handleDashboardInitialized = function (args) {
};
